package com.jason.campusinn.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.yuandian.wanna.R;
import com.yuandian.wanna.WannaApp;
import com.yuandian.wanna.utils.DisplayUtil;

/**
 * Created by yuandian on 2015/5/22.
 */
public class ChooseSizeView extends LinearLayout implements View.OnClickListener {
    @ViewInject(R.id.choose_size_tv_type_name)
    private TextView mTvTypeName;
    @ViewInject(R.id.choose_size_iv_type_image)
    private ImageView mIvTypeImage;
    @ViewInject(R.id.choose_size_tv_reduce)
    private TextView mTvReduce;
    @ViewInject(R.id.choose_size_tv_plus)
    private TextView mTvPlus;
    @ViewInject(R.id.choose_size_tv_size)
    private TextView mTvSize;
    @ViewInject(R.id.choose_size_tv_cm)
    private TextView mTvCM;
    private int mSize;

    public ChooseSizeView(Context context) {
        super(context,null);
    }

    public ChooseSizeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(context, attrs);
    }

    private void initViews(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_choose_size,null);
        addView(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        ViewUtils.inject(this, view);
        mTvReduce.setOnClickListener(this);
        mTvPlus.setOnClickListener(this);
        TypedArray array = context.getResources().obtainAttributes(attrs,
                R.styleable.choose_size_view);
        mSize = array.getInteger(R.styleable.choose_size_view_value,0);
        mIvTypeImage.setImageResource(array.getResourceId(R.styleable.choose_size_view_type_icon, R.drawable.icon_sleeve_size));
        mTvTypeName.setText(array.getString(R.styleable.choose_size_view_type_text));
        mTvSize.setText(mSize + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.choose_size_tv_reduce:
                if (mSize>-2){
                    mSize--;
                }
                break;
            case R.id.choose_size_tv_plus:
                if (mSize<2){
                    mSize++;
                }
                break;
        }
        if (mSize>0){
            mTvSize.setText("+"+mSize + "");
        }else {
            mTvSize.setText(mSize+"");
        }

    }

    /**设置类型背景图*/
    public void setTypeBackGround(int resourceId){
        mIvTypeImage.setImageResource(resourceId);
    }

    /**设置类型文字说明*/
    public void setTypeText(String type){
        mTvTypeName.setText(type);
    }

    /**设置类型文字颜色*/
    public void setTypeTextColor(int color){
        mTvTypeName.setTextColor(getResources().getColor(color));
    }

    /**设置类型文字大小*/
    public void setTypeTextSize(int size ,boolean isDip){
        if (isDip){
            size = DisplayUtil.sp2px(size, WannaApp.getInstance().mScaledDensity);
        }
        mTvTypeName.setTextSize(size);
    }

    /**设置数值*/
    public void setValue(int value){
        mSize = value;
        mTvSize.setText(mSize + "");
    }

    /**设置数值文字颜色*/
    public void setValueTextColor(int color){
        mTvSize.setTextColor(getResources().getColor(color));
        mTvCM.setTextColor(getResources().getColor(color));
    }



    public int getSize(){
        String sizeString = mSize+"";
        return mSize;
//        return TextUtils.isEmpty(sizeString)?0:Integer.valueOf(sizeString);
    }
}
